// pages/search/search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history:[],
    hot:[],
    keyword:'',
    froms:'index'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.IsFlash = false;
    this.setData({
      history: wx.getStorageSync('history') || [],
      froms:options.froms
    })
    this.Find_Hot();
    
  },
  Find_Hot: function () {
    var that = this;
    var data = {};
    data.act = "Find_Hot";
    app.Ajax(data, function (json) {
      that.setData({
        hot: json.data
      });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  openSearch:function(){
    app.IsFlash = true;
    if (this.data.keyword.trim()==""){
      app.showError("请输入关键词");
      return;
    }
    app.Keyword=this.data.keyword;
    var history = wx.getStorageSync('history') || []
    history.unshift(app.Keyword)
    wx.setStorageSync('history', history)
    if (this.data.froms == "index") {
      wx.navigateTo({
        url: '/pages/product/product?order=allrank',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  getKeyword:function(e){
    this.setData({
      keyword: e.detail.value
    })
  },
  quickSearch:function(e){
    app.IsFlash = true;
    app.Keyword = e.currentTarget.dataset.keyword
    if(this.data.froms=="index"){
      wx.navigateTo({
        url: '/pages/product/product?order=allrank',
      })
    }else{
      wx.navigateBack({
        delta: 1
      })
    }
  },
  deleteHistory:function(e){
    var that=this;
    wx.showModal({
      title: '提示',
      confirmColor:'#4f8bed',
      content: '您确定要删除历史搜索吗？',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync('history', []);
          that.setData({history:[]});
        }
      }
    })
  }
})